package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.data.QueryCustomerStatisticsRequest;
import com.travel.server.manager.dto.response.customer.data.QueryCustomerStatisticsResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.dal.data.CustomerDataDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.dto.data.CustomerDataDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 10:47
 * @Desc:
 */
@Component
@Slf4j
public class QueryCustomerStatisticsCommand extends AbstractCommand<QueryCustomerStatisticsRequest, QueryCustomerStatisticsResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private CustomerDataDal customerDataDal;

    @Override
    protected void checkInput(QueryCustomerStatisticsRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryCustomerStatisticsResponse handle(QueryCustomerStatisticsRequest request) {
        checkInput(request);

        MemberGradeInfo member = customerDataDal.getMember();
        MemberGradeInfo highMember = customerDataDal.getHighMember();

        DataDateVo dateVo = OrderDataSal.getDate(request.getType(), request);
        Date startTime = dateVo.getStartTime();
        Date endTime = dateVo.getEndTime();
        Date lastTime = dateVo.getLastTime();

        List<CustomerDataDto> list = customerDal.selectCustomerData(null ,endTime, member.getId(), highMember.getId());

        return makeResponse(lastTime, startTime, endTime, list);
    }

    private QueryCustomerStatisticsResponse makeResponse(Date lastTime, Date startTime, Date endTime, List<CustomerDataDto> list){
        Integer currentMember = 0;
        Integer lastMember = 0;
        Integer currentNewMember = 0;
        Integer lastNewMember = 0;
        Integer currentHighMember = 0;
        Integer lastHighMember = 0;
        if(!CollectionUtil.isEmpty(list)){
            for(CustomerDataDto dto : list){
                currentMember += dto.getMember();
                currentHighMember += dto.getHighMember();
                Date date = DateUtil.getDataTime(dto.getDays());
                if(date.before(startTime)){
                    lastMember += dto.getMember();
                    lastHighMember += dto.getHighMember();
                }
                if(!date.before(lastTime) && date.before(startTime)){
                    lastNewMember += dto.getMember();
                }
                if(!date.before(startTime) && date.before(endTime)){
                    currentNewMember += dto.getMember();
                }
            }
        }
        QueryCustomerStatisticsResponse response = new QueryCustomerStatisticsResponse();
        response.setHighMember(currentHighMember);
        response.setHighMemberCompare(OrderDataSal.getProportion(currentHighMember, lastHighMember));
        response.setMember(currentMember);
        response.setMemberCompare(OrderDataSal.getProportion(currentMember, lastMember));
        response.setNewMember(currentNewMember);
        response.setNewMemberCompare(OrderDataSal.getProportion(currentNewMember, lastNewMember));
        return response;
    }
}
